package com.bootdo.app.controller;

import com.bootdo.app.domain.PayeeAppDO;
import com.bootdo.app.service.AppLoginService;
import com.bootdo.common.configure.controller.BaseController;
import com.bootdo.common.utils.R;
import com.bootdo.order.domain.OrderPayDO;
import com.bootdo.payee.domain.PassagewayDO;
import com.bootdo.payee.domain.PaymentCodeDO;
import com.bootdo.order.domain.ReceiptDO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/app")
public class AppLoginController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AppLoginService appLoginService;

    /**
     * 登陆
     * @param userName
     * @param password
     * @return
     */
    @PostMapping("/login")
    public R login(String userName, String password, String deviceId){
        try {
            String userId = appLoginService.login(userName, password, deviceId);
            return R.ok("操作成功", userId);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(-1, e.getMessage());
        }
    }

    /**
     * 获取用户基本信息
     * @param userId
     * @return
     */
    @PostMapping("/findUser")
    public R findUser(String userId){
        try {
            PayeeAppDO payeeApp = appLoginService.findUser(userId);
            return R.ok(payeeApp);
        } catch (Exception e) {
            logger.error("======获取用户基本信息======" + e);
            return R.error(-1, e.getMessage());
        }

    }

    /**
     * 修改用户自动接单状态
     * @param userId
     * @return
     */
    @PostMapping("/updateStatus")
    public R updateStatus(String userId, String status, String deviceId) {
        try {
            int count = appLoginService.updateStatus(userId, status, deviceId);
            if (count > 0) {
                return R.ok();
            }
            if (count == -2){
                return R.error(-2, "当前用户登录设备异常，请联系管理员");
            }
            if (count == 99){
                return R.error(-99, "系统异常");
            }
            return R.error();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(-1, e.getMessage());
        }

    }



    /**
     * 当前使用通道列表
     * @param passageway
     * @return
     */
    @PostMapping("/userPassagewayList")
    public R userPassagewayList(PassagewayDO passageway) {
        try {
            return appLoginService.userPassagewayList(passageway);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(">>>>>>>>>>userPassagewayList接口报错:未知错误-{}，错误栈-{}<<<<<<<<<<",e.getMessage(), e);
            return R.error(-1,"系统繁忙");
        }
    }

    /**
     * 关闭/开启当前通道
     * @param passageway
     * @return
     */
    @PostMapping("/editPassagewayStatus")
    public R editPassagewayStatus(PassagewayDO passageway) {
        try {
            if (appLoginService.editPassagewayStatus(passageway) > 0){
                return R.ok();
            }
            return R.error(-1 , "关闭/开启通道失败");
        }catch (Exception e){
            e.printStackTrace();
            logger.error(">>>>>>>>>>editPassagewayStatus接口报错:未知错误-{}，错误栈-{}<<<<<<<<<<",e.getMessage(), e);
            return R.error(-1,"系统繁忙");
        }
    }

    /**
     * 通道新增
     * @param passageway
     * @return
     */
    @PostMapping("/passagewaySave")
    public R passagewaySave(PassagewayDO passageway) {
        try {
            if (appLoginService.passagewaySave(passageway) > 0){
                return R.ok();
            }
            return R.error(-1 , "通道新增失败");
        }catch (Exception e){
            e.printStackTrace();
            logger.error(">>>>>>>>>>passagewaySave接口报错:未知错误-{}，错误栈-{}<<<<<<<<<<",e.getMessage(), e);
            return R.error(-1,"系统繁忙");
        }
    }

    /**
     *  删除通道
     * @param passageway
     * @return
     */
    @PostMapping("/passagewayDelete")
    public R passagewayDelete(PassagewayDO passageway) {
        try {
            if (appLoginService.passagewayDelete(passageway) > 0){
                return R.ok();
            }
            return R.error(-1 , "删除通道失败");
        }catch (Exception e){
            e.printStackTrace();
            logger.error(">>>>>>>>>>passagewayDelete接口报错:未知错误-{}，错误栈-{}<<<<<<<<<<",e.getMessage(), e);
            return R.error(-1,"系统繁忙");
        }
    }

    /**
     * 收款码新增
     * @param paymentCode
     * @return
     */
    @PostMapping("/paymentCodeSave2")
    public R paymentCodeSave(PaymentCodeDO paymentCode) {
        try {
            return appLoginService.paymentCodeSave(paymentCode);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(">>>>>>>>>>paymentCodeSave接口报错:未知错误-{}，错误栈-{}<<<<<<<<<<",e.getMessage(), e);
            return R.error(-1,"系统繁忙");
        }
    }

    /**
     * 收款码新增2
     * @param paymentCode
     * @return
     */
    @PostMapping("/paymentCodeSave")
    public R paymentCodeSave2(PaymentCodeDO paymentCode) {
        try {
            int count = appLoginService.paymentCodeSave2(paymentCode);
            if (count > 0){
                return R.ok();
            }
            return R.error(-1, "新增收款码失败");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(-1, e.getMessage());
        }
    }

//    @PostMapping("/aaa")
//    public R aaa(){
//        appLoginService.aaa();
//        return R.ok();
//    }

    /**
     *  删除收款码
     * @param paymentCode
     * @return
     */
    @PostMapping("/paymentCodeDelete")
    public R paymentCodeDelete(PaymentCodeDO paymentCode) {
        try {
            if (appLoginService.paymentCodeDelete(paymentCode) > 0){
                return R.ok();
            }
            return R.error(-1 , "删除收款码失败");
        }catch (Exception e){
            e.printStackTrace();
            logger.error(">>>>>>>>>>paymentCodeDelete接口报错:未知错误-{}，错误栈-{}<<<<<<<<<<",e.getMessage(), e);
            return R.error(-1,"系统繁忙");
        }
    }

    /**
     * 关闭/开启当前收款码
     * @param paymentCode
     * @return
     */
    @PostMapping("/editPaymentCodeRecAcc")
    public R editPaymentCodeRecAcc(PaymentCodeDO paymentCode) {
        try {
            if (appLoginService.editPaymentCodeRecAcc(paymentCode) > 0){
                return R.ok();
            }
            return R.error(-1 , "关闭/开启收款码失败");
        }catch (Exception e){
            e.printStackTrace();
            logger.error(">>>>>>>>>>editPaymentCodeRecAcc接口报错:未知错误-{}，错误栈-{}<<<<<<<<<<",e.getMessage(), e);
            return R.error(-1,"系统繁忙");
        }
    }

    /**
     * 当前使用收款码列表
     * @param paymentCode
     * @return
     */
    @PostMapping("/userPassagewayCodeList")
    public R userPassagewayCodeList(PaymentCodeDO paymentCode) {
        try {
            return appLoginService.userPassagewayCodeList(paymentCode);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(">>>>>>>>>>userPassagewayCodeList接口报错:未知错误-{}，错误栈-{}<<<<<<<<<<",e.getMessage(), e);
            return R.error(-1,"系统繁忙");
        }
    }

    /**
     * 订单查询
     * @param orderPay
     * @return
     */
    @PostMapping("/queryUserOrderPayList")
    public R queryUserOrderPayList(OrderPayDO orderPay) {
        try {
            return appLoginService.queryUserOrderPayList(orderPay);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(">>>>>>>>>>queryUserOrderPayList接口报错:未知错误-{}，错误栈-{}<<<<<<<<<<",e.getMessage(), e);
            return R.error(-1,"系统繁忙");
        }
    }

    /**
     * 查询当前用户订单收入数据
     * @param orderPay
     * @return
     */
    @PostMapping("/queryUserOrderPayIncomeData")
    public R queryUserOrderPayIncomeData(OrderPayDO orderPay) {
        try {
            return appLoginService.queryUserOrderPayIncomeData(orderPay);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(">>>>>>>>>>queryUserOrderPayIncomeData接口报错:未知错误-{}，错误栈-{}<<<<<<<<<<",e.getMessage(), e);
            return R.error(-1,"系统繁忙");
        }
    }


    /**
     * 当前用户查询收款户代理信息
     * @param userId
     * @return
     */
    @PostMapping("/queryPayeeAgentBankByPayeeId")
    public R queryPayeeAgentBankByPayeeId(String userId) {
        try {
            return appLoginService.queryPayeeAgentBankByPayeeId(Long.valueOf(userId));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(">>>>>>>>>>queryPayeeAgentBankByPayeeId接口报错:未知错误-{}，错误栈-{}<<<<<<<<<<",e.getMessage(), e);
            return R.error(-1,"系统繁忙");
        }
    }


    /**
     * 当前用户查询收款户代理信息
     * @param orderId
     * @return
     */
    @PostMapping("/queryPayeeAgentBankByOrderId")
    public R queryPayeeAgentBankByOrderId(String orderId) {
        try {
            return appLoginService.queryPayeeAgentBankByOrderId(orderId);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(">>>>>>>>>>queryPayeeAgentBankByPayeeId接口报错:未知错误-{}，错误栈-{}<<<<<<<<<<",e.getMessage(), e);
            return R.error(-1,"系统繁忙");
        }
    }

    /**
     * 上传凭证
     * @param receipt
     * @return
     */
    @PostMapping("/uploadReceipt")
    public R uploadReceipt(ReceiptDO receipt) {
        try {
            return appLoginService.uploadReceipt(receipt);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(">>>>>>>>>>uploadReceipt接口报错:未知错误-{}，错误栈-{}<<<<<<<<<<",e.getMessage(), e);
            return R.error(-1,"系统繁忙");
        }
    }

    /**
     * 确认收款
     * @param orderPay
     * @return
     */
    @PostMapping("/confirmReceipt")
    public R confirmReceipt(OrderPayDO orderPay) {
        try {
            int count = appLoginService.confirmReceipt(orderPay);
            if (count == -2){
                return R.error(-1, "请勿重复点击确认收款");
            } else if (count == -3){
                return R.error(-1, "当前订单非未付款状态");
            } else if (count > 0){
                return R.ok();
            } else {
                return R.error(-1, "确认收款失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error(">>>>>>>>>>confirmReceipt接口报错:未知错误-{}，错误栈-{}<<<<<<<<<<",e.getMessage(), e);
            return R.error(-1,"系统繁忙");
        }
    }

    /**
     * 余额明细
     * @param userId
     * @return
     */
    @PostMapping("/detailsOfBalance")
    public R detailsOfBalance(String userId, String startTime, String endTime) {
        try {
            return appLoginService.detailsOfBalance(userId,startTime,endTime);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(">>>>>>>>>>detailsOfBalance接口报错:未知错误-{}，错误栈-{}<<<<<<<<<<",e.getMessage(), e);
            return R.error(-1,"系统繁忙");
        }
    }

    /**
     * 退出登录
     * @param userId
     * @return
     */
    @PostMapping("/logout")
    public R logout(String userId) {
        try {
            return appLoginService.logout(userId);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(">>>>>>>>>>logout接口报错:未知错误-{}，错误栈-{}<<<<<<<<<<",e.getMessage(), e);
            return R.error(-1,"系统繁忙");
        }
    }


    /**
     * 修改密码
     * @param userId
     * @return
     */
    @PostMapping("/changePassword")
    public R changePassword(String userId,String newPassword,String oldPassword, String deviceId) {
        try {
            return appLoginService.changePassword(userId,newPassword,oldPassword,deviceId);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(">>>>>>>>>>changePassword接口报错:未知错误-{}，错误栈-{}<<<<<<<<<<",e.getMessage(), e);
            return R.error(-1,"系统繁忙");
        }
    }

    /**
     * 用户信息
     * @param userId
     * @return
     */
    @PostMapping("/personalInformation")
    public R personalInformation(String userId) {
        try {
            return appLoginService.personalInformation(userId);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(">>>>>>>>>>personalInformation接口报错:未知错误-{}，错误栈-{}<<<<<<<<<<",e.getMessage(), e);
            return R.error(-1,"系统繁忙");
        }
    }


    /**
     * app更新
     * @return
     */
    @PostMapping("/appUpdate")
    public R appUpdate() {
        try {
            return appLoginService.appUpdate();
        }catch (Exception e){
            e.printStackTrace();
            logger.error(">>>>>>>>>>appUpdate接口报错:未知错误-{}，错误栈-{}<<<<<<<<<<",e.getMessage(), e);
            return R.error(-1,"系统繁忙");
        }
    }

    /**
     * 接单状态
     * @return
     */
    @PostMapping("/receiptState")
    public R receiptState(String userId) {
        try {
            return appLoginService.receiptState(userId);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(">>>>>>>>>>receiptState接口报错:未知错误-{}，错误栈-{}<<<<<<<<<<",e.getMessage(), e);
            return R.error(-1,"系统繁忙");
        }
    }






}
